package com.af.security.auth;


import com.af.security.auth.user.data.AfAuthService;
import com.af.security.auth.user.detail.AuthMenu;
import com.af.security.auth.user.detail.AuthUser;
import com.af.security.property.ProjectProperties;
import com.af.system.api.result.AfResult;
import com.af.system.api.result.AfResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhenyun.su
 * @comment: 是否已认证： http://localhost:8081/auth/isAuthenticated
 * 认证用户： http://localhost:8081/auth/user
 * 认证角色： http://localhost:8081/auth/role
 * 认证菜单： http://localhost:8081/auth/menu
 * 认证权限： http://localhost:8081/auth/authority
 * 会话信息： http://localhost:8081/auth/session
 * 认证数据： http://localhost:8081/auth/authentication
 * @since : 2020/03/25
 */

@RestController
@RequestMapping("auth")
//@Api(value = "认证&授权数据",tags = "用户认证中心")
public class AfAuthController {
    private static final Logger logger = LoggerFactory.getLogger(AfAuthController.class);
    private AfAuthService authService = new AfAuthService();
    @Autowired
    private ProjectProperties projectProperties;

    @GetMapping(value = "isAuthenticated")
    public AfResult<String> isAuthenticated() {
        if (authService.isAuthenticated()) {
            return AfResultUtils.success("Authentication is successful",null);
        } else {
            return AfResultUtils.failure("Authentication failed", null);
        }
    }

    @GetMapping("app")
    public AfResult<Map<String, Object>> getApp(
            @RequestParam(required = false, defaultValue = "true") Boolean menus
            , @RequestParam(required = false, defaultValue = "true") Boolean role
            , @RequestParam(required = false, defaultValue = "true") Boolean authority) {
        if (authService.isAuthenticated()) {
            Map<String, Object> appInfo = new HashMap<>();
            appInfo.put("user", authService.getAuthUser());

            Map<String, Object> client = new HashMap<>();
            client.put("id", projectProperties.getName());
            client.put("name", projectProperties.getName());
            client.put("description", "af application");

            appInfo.put("app", client);
            if (menus) {
                appInfo.put("menus", authService.getAuthMenu());
            }
            if (role) {
                appInfo.put("role", authService.getAuthRole());
            }
            if (authority) {
                appInfo.put("authority", authService.getAuthorities());
            }
            return AfResultUtils.success(appInfo);
        } else {
            return AfResultUtils.failure(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
        }
    }

    @GetMapping("user")
    public AfResult<AuthUser> getAuthUser() {
        return AfResultUtils.success(authService.getAuthUser());
    }

    @GetMapping("role")
    public AfResult<List<String>> getAuthRole() {
        return AfResultUtils.success(authService.getAuthRole());
    }

    @GetMapping("menu")
    public AfResult<List<AuthMenu>> getAuthMenu() {
        return AfResultUtils.success(authService.getAuthMenu());
    }

    @GetMapping("authority")
    public AfResult<List<String>> getAuthorities() {
        return AfResultUtils.success(authService.getAuthorities());
    }

    @GetMapping("session")
    public HttpSession getAuthorities(HttpSession session) {
        return session;
    }

    @GetMapping("principal")
    public Principal getPrincipal(Principal principal) {
        return principal;
    }
    @GetMapping("authentication")
    public Authentication getPrincipal(Authentication authentication) {
        return authentication;
    }
}
